import 'dart:typed_data';
import 'dart:ui' as ui;

import 'package:flutter/material.dart';
import 'package:flutter/painting.dart';
import 'package:flutter/rendering.dart';
import 'package:hy/common/values/color_value.dart';
import 'package:hy/common/values/values.dart';

class HomeImageEditPage extends StatefulWidget {
  final String imagePath;
  const HomeImageEditPage({Key? key, required this.imagePath})
      : super(key: key);

  @override
  State<HomeImageEditPage> createState() => _HomeImageEditPageState();
}

class _HomeImageEditPageState extends State<HomeImageEditPage> {
  /// 生成海报key
  final GlobalKey _globalKey = GlobalKey();

  /// 滤镜集合
  int selectedIndex = 0;
  final List<List<double>> filters = [
    SEPIA_MATRIX,
    // GREYSCALE_MATRIX,
    // VINTAGE_MATRIX,
    SWEET_MATRIX
  ];

  /// 改变滤镜
  _changeFilterModel(int index) {
    selectedIndex = index;
    return filters[index];
  }

  /// 生成加滤镜之后的图片(发布)
  void convertWidgetToImage() async {
    RenderRepaintBoundary? repaintBoundary =
        _globalKey.currentContext?.findRenderObject() as RenderRepaintBoundary;
    ui.Image? boxImage = await repaintBoundary.toImage(pixelRatio: 1);
    ByteData? byteData =
        await boxImage.toByteData(format: ui.ImageByteFormat.png);
    Uint8List? uint8list = byteData?.buffer.asUint8List();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.white,
        elevation: 0,
        leading: InkWell(
          onTap: () {
            Navigator.of(context).pop();
          },
          child: Icon(
            Icons.arrow_back_ios_new_rounded,
            size: 24,
            color: AppColor.colorLightGrey,
          ),
        ),
        actions: [
          Padding(
            padding: const EdgeInsets.only(
              top: 12,
              right: 12,
            ),
            child: Text(
              "发布",
              style: TextStyle(
                fontSize: 16,
                foreground: _paintText(context),
              ),
            ),
          ),
        ],
      ),
      body: Column(
        children: [
          Expanded(
            child: Padding(
              padding: const EdgeInsets.all(10),
              child: RepaintBoundary(
                key: _globalKey,
                child: ColorFiltered(
                  colorFilter:
                      ColorFilter.matrix(_changeFilterModel(selectedIndex)),
                  child: Image.asset(
                    widget.imagePath,
                    fit: BoxFit.cover,
                  ),
                ),
              ),
            ),
          ),
          SizedBox(
            height: 200,
            child: Row(
              children: [
                ElevatedButton(
                  onPressed: () {
                    setState(() {
                      selectedIndex = 0;
                    });
                  },
                  child: Image.asset(
                    "assets/images/lv001.png",
                    width: 90,
                    height: 160,
                    fit: BoxFit.cover,
                  ),
                ),
                const SizedBox(
                  width: 10,
                ),
                ElevatedButton(
                  onPressed: () {
                    setState(() {
                      selectedIndex = 1;
                    });
                  },
                  child: Image.asset(
                    "assets/images/lv002.png",
                    width: 90,
                    height: 160,
                    fit: BoxFit.cover,
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  /// 文字渐变色
  Paint? _paintText(BuildContext context) {
    Size size = MediaQuery.of(context).size;
    Gradient gradient = LinearGradient(
      colors: [AppColor.colorLightCyan, AppColor.colorDarkCyan],
      begin: Alignment.topLeft,
      end: Alignment.bottomRight,
    );
    Shader shader =
        gradient.createShader(Rect.fromLTWH(0, 0, size.width, size.height));

    Paint paint = Paint()..shader = shader;

    return paint;
  }
}
